package cc.smarnet.caihou.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.io.Serializable;
import java.util.List;

import cc.smarnet.caihou.R;
import cc.smarnet.caihou.entity.Warehouse;
import cc.smarnet.caihou.phone.warehouse.WarehouseDetailActivity;

/**
* 入库信息的adapter
* */
public class WarehouseAdapter extends RecyclerView.Adapter {
    private Context context;
    private List<Warehouse> list;

    public WarehouseAdapter(Context context, List<Warehouse> list){
        this.context=context;
        this.list=list;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.warehouse_item,parent,false);
        WarehouseViewHolder warehouseViewHolder=new WarehouseViewHolder(view);
        return warehouseViewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        Warehouse warehouse=list.get(position);
        WarehouseViewHolder WarehouseViewHolder= (WarehouseViewHolder) holder;
        WarehouseViewHolder.createTime.setText(warehouse.getCreateTime());
        WarehouseViewHolder.createUser.setText(warehouse.getCreateUser());
        WarehouseViewHolder.flowerPoleLength.setText(warehouse.getFlowerPoleLength());
        WarehouseViewHolder.packingSpecifications.setText(warehouse.getPackingSpecifications());
        WarehouseViewHolder.sortTime.setText(warehouse.getSortTime());
        WarehouseViewHolder.baseName.setText(warehouse.getBaseName());
        WarehouseViewHolder.greenhouseName.setText(warehouse.getGreenhouseName());
        WarehouseViewHolder.tagNum.setText(String.valueOf(warehouse.getTagNum()));
        WarehouseViewHolder.variety.setText(warehouse.getVariety());
        WarehouseViewHolder.varietyCode.setText(warehouse.getVarietyCode());
        WarehouseViewHolder.classes.setText(warehouse.getClasses());

    }

    @Override
    public int getItemCount() {
        // list.size()为0的话也必须至少返回1，返回有数值的话才会显示item，因为该方法不会返回0
        return list.size() > 0 ? list.size() : 1;
    }

    class WarehouseViewHolder extends RecyclerView.ViewHolder{
        TextView createTime;
        TextView createUser;
        TextView flowerPoleLength;
        TextView packingSpecifications;
        TextView sortTime;
        TextView baseName;
        TextView greenhouseName;
        TextView tagNum;
        TextView variety;
        TextView varietyCode;
        TextView classes;

        public WarehouseViewHolder(@NonNull View itemView) {
            super(itemView);
            createTime=itemView.findViewById(R.id.warehouse_item_createTime);
            createUser=itemView.findViewById(R.id.warehouse_item_createUser);
            flowerPoleLength=itemView.findViewById(R.id.warehouse_item_flowerPoleLength);
            packingSpecifications=itemView.findViewById(R.id.warehouse_item_packingSpecifications);
            sortTime=itemView.findViewById(R.id.warehouse_item_sortTime);
            baseName=itemView.findViewById(R.id.warehouse_item_baseName);
            greenhouseName=itemView.findViewById(R.id.warehouse_item_greenhouseName);
            tagNum=itemView.findViewById(R.id.warehouse_item_tagNum);
            variety=itemView.findViewById(R.id.warehouse_item_variety);
            varietyCode=itemView.findViewById(R.id.warehouse_item_varietyCode);
            classes=itemView.findViewById(R.id.warehouse_item_classes);

            // item点击事件
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Warehouse warehouse=list.get(getAdapterPosition());
                    Intent intent = new Intent(context, WarehouseDetailActivity.class);
                    intent.putExtra("warehouseDetail", (Serializable) warehouse);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
                    context.startActivity(intent);
                }
            });
        }
    }
}
